package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.TDutyMapper;
import edu.gxnu.cetcdemo.pojo.TDuty;
import edu.gxnu.cetcdemo.service.TDutyService;
import edu.gxnu.cetcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Uplifted
 * @description 针对表【t_duty】的数据库操作Service实现
 * @createDate 2024-04-02 15:09:11
 */
@Service
public class TDutyServiceImpl extends ServiceImpl<TDutyMapper, TDuty>
        implements TDutyService{

    @Autowired
    private TDutyMapper tDutyMapper;

    @Override
    public Result queryTDuty() {
        Map data = new HashMap();
        List<TDuty> list = tDutyMapper.selectList(null);
        data.put("tDutyList", list);
        return Result.build(200, "成功查询所有信息", data);
    }

    @Override
    public Result addTDuty(TDuty tDuty) {
        // 是否存在相同信息的记录
        LambdaQueryWrapper<TDuty> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDuty::getDutyName, tDuty.getDutyName())
                .eq(TDuty::getDutyPhone, tDuty.getDutyPhone())
                .eq(TDuty::getDutyTime, tDuty.getDutyTime())
                .eq(TDuty::getDutyWeek, tDuty.getDutyWeek());
        TDuty tTDuty = tDutyMapper.selectOne(lambdaQueryWrapper);

        if(tTDuty != null){
            return Result.build(501, "该信息已存在");
        }

        tDutyMapper.insert(tDuty);
        return Result.build(200, "成功添加信息");
    }

    @Override
    public Result updateTDuty(TDuty tDuty) {
        // 是否存在相同信息的记录
        LambdaQueryWrapper<TDuty> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TDuty::getDutyName, tDuty.getDutyName())
                .eq(TDuty::getDutyPhone, tDuty.getDutyPhone())
                .eq(TDuty::getDutyTime, tDuty.getDutyTime())
                .eq(TDuty::getDutyWeek, tDuty.getDutyWeek());
        TDuty tTDuty = tDutyMapper.selectOne(lambdaQueryWrapper);

        if(tTDuty != null){
            return Result.build(501, "存在相同信息");
        }

        tDutyMapper.update(tDuty, new LambdaQueryWrapper<TDuty>().eq(TDuty::getDutyId, tDuty.getDutyId()));
        return Result.build(200, "成功更新信息");
    }

    @Override
    public Result delTDuty(long id) {
        LambdaQueryWrapper<TDuty> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        tDutyMapper.delete(lambdaQueryWrapper.eq(TDuty::getDutyId, id));
        return Result.build(200, "成功删除信息");
    }
}




